﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;

namespace SRS
{
    /// <summary>
    /// Interaction logic for Editor.xaml
    /// </summary>
    public partial class Editor : UserControl
    {
        private MemoryStream stream;
        public MemoryStream Stream
        {
            get { return stream; }
        }

        private String fileName;
        public String FileName
        {
            get { return fileName; }
        }

        public TextBox TextBox
        {
            get { return textBox; }
            set { textBox = value; }
        }

        public Editor()
        {
            InitializeComponent();
        }

        #region Event Click
        private void buttonNew_Click(object sender, RoutedEventArgs e)
        {
            clear();
        }

        private void buttonOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.RestoreDirectory = true;
            String fileName = null;

            // Show Open File Dialog
            if (ofd.ShowDialog() == true)
            {
                fileName = ofd.FileName;
                if (fileName != null)
                {
                    // read file
                    readFile(fileName);

                    // Set filename
                    this.fileName = fileName;
                }
            }
        }
        #endregion

        private void readFile(string fileName)
        {
            // Make File Stream
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            // Create temporary
            long length = fs.Length;
            byte[] temp = new byte[length];

            // Read File
            fs.Position = 0;
            fs.Read(temp, 0, (int)length);

            // Set stream
            stream = new MemoryStream(temp);

            // Set text box
            textBox.Text = Helper.ArrayByteToString(stream.ToArray());
        }

        private void clear()
        {
            textBox.Text = "";

            if (stream != null)
            {
                stream.Flush();
                stream.Close();
                stream = null;
            }
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.RestoreDirectory = true;
            String fileName = null;

            // Show save file dialog
            if (sfd.ShowDialog() == true)
            {
                fileName = sfd.FileName;

                if (fileName != null)
                {
                    // Save file
                    saveFile(fileName);

                    // Set file name
                    this.fileName = fileName;
                }
            }
        }

        private void saveFile(String fileName)
        {
            if (fileName != null)
            {
                stream = new MemoryStream(Helper.StringToArrayByte(textBox.Text));
                SaveMemoryStream(stream, fileName);
            }
        }

        private void SaveMemoryStream(MemoryStream ms, String fileName)
        {
            FileStream outStream = File.OpenWrite(fileName);
            ms.WriteTo(outStream);
            outStream.Flush();
            outStream.Close();
        }
    }
}
